package com.example.qxfw.dzzh.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.qxfw.common.entity.MapPoint;
import com.example.qxfw.common.entity.PotentialPoints;
import com.example.qxfw.common.util.DmgisUtils;
import com.example.qxfw.dzzh.mapper.PotentialPointMapper;
import com.example.qxfw.dzzh.service.PotentialPointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

@Service
public class PotentialPointServiceImpl implements PotentialPointService {

    @Autowired
    private PotentialPointMapper potentialPointMapper;

    @Override
    public List<PotentialPoints> listByParam(String ppId, String ppName, String country, String disType, String disScale, String disLevel, Double threatPop1, Double threatPop2, Double threatProp1, Double threatProp2) {
        return potentialPointMapper.selectList(Wrappers.<PotentialPoints>lambdaQuery()
                .eq(StringUtils.isNotBlank(ppName), PotentialPoints::getPpId, ppId)
                .eq(StringUtils.isNotBlank(ppName), PotentialPoints::getPpName, ppName)
                .eq(StringUtils.isNotBlank(country) && !country.equals("全市"), PotentialPoints::getCountry, country)
                .eq(StringUtils.isNotBlank(disType) && !disType.equals("全部"), PotentialPoints::getDisType, disType)
                .eq(StringUtils.isNotBlank(disScale) && !disScale.equals("全部"), PotentialPoints::getDisScale, disScale)
                .eq(StringUtils.isNotBlank(disLevel) && !disLevel.equals("全部"), PotentialPoints::getDisLevel, disLevel)
                .ge(ObjectUtils.isNotEmpty(threatPop1), PotentialPoints::getThreatPop, threatPop1)
                .le(ObjectUtils.isNotEmpty(threatPop2), PotentialPoints::getThreatPop, threatPop2)
                .ge(ObjectUtils.isNotEmpty(threatProp1), PotentialPoints::getThreatProp, threatProp1)
                .le(ObjectUtils.isNotEmpty(threatProp2), PotentialPoints::getThreatProp, threatProp2));
    }

    @Override
    public List<PotentialPoints> listByDistance(double lon, double lat, double distance) {
        return potentialPointMapper.listByDistance(lon, lat, distance);
    }

    @Override
    public List<PotentialPoints> listByPolygon(List<List<Double>> range) {
        range.remove(range.size() - 1);
        List<PotentialPoints> potentialPoints = potentialPointMapper.selectList(null);
        Iterator<PotentialPoints> iterator = potentialPoints.iterator();
        while (iterator.hasNext()) {
            PotentialPoints next = iterator.next();
            MapPoint mapPoint = new MapPoint();
            mapPoint.setLat(next.getLat());
            mapPoint.setLon(next.getLon());
            if (!DmgisUtils.isScope(mapPoint, range)) {
                iterator.remove();
            }
        }
        return potentialPoints;
    }

    @Override
    public List<PotentialPoints> countByParams(String country, String disType, String content) {
        return potentialPointMapper.selectList(Wrappers.<PotentialPoints>query()
                .select("country", content, "count(*) as field_id")
                .lambda()
                .eq(StringUtils.isNotBlank(country) && !country.equals("全市"), PotentialPoints::getCountry, country)
                .eq(StringUtils.isNotBlank(disType) && !disType.equals("全部"), PotentialPoints::getDisType, disType)
                .last("group by country,"+content));
    }
}
